package com.hqyj.springCloundAccount.dao;


import com.hqyj.account.entity.Role;
import com.hqyj.commom.vo.Search;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface RoleDao2 {

    @Insert("insert into test_role (create_date,update_date,role_name) values (#{createDate},#{updateDate},#{roleName})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void addRole(Role role);



    @Select("select * from test_role where role_name = #{roleName}")
    Role getRoleByRoleName(String roleName);

    @Select("select * from test_role where id = #{id}")
    Role getRoleByRoleId(int id);


    @Update("update test_role set update_Date = #{updateDate} , role_name = #{roleName} where id = #{id}")
    void updateRole(Role role);

    @Delete("delete from test_role where id =#{id}")
    void deleteRoleById(int id);



    @Select("select * from test_role as tr left join test_user_role as tur on tr.id = tur.role_id where tur.user_id = #{userId}")
    List<Role> getRolesByUserId(int userId);

    @Select("select * from test_role as tr left join test_role_resource as trr on tr.id = trr.role_id where trr.resporce_id = #{resourceId}")
    List<Role> getRolesByResourceId(int resourceId);

    @Select("select * from test_role")
    List<Role> getRoles();

    @Select("<script>"
            + "select * from test_role "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (role_name like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='sort != \"\" and sort != null'>"
            + " order by ${sort} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<Role> getRolesBySearch(Search search);











}
